package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortDartTypeInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogBeautyEscorting;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY_FROM_CROSS, desc = "接镖+派美女")
public class BeautyEscortAcceptDispatchCmdFromCross extends AbstractCommand {

    private int client;
    private int crossRet;
    private int typeId;
    private List<Integer> buyList;
    private List<EscortBeautyDispatch> beautyDispatchList;
    private int cartId;
    private String buyBeautyConsume;
    private long dartId;
    private boolean autoAccelerate;
    private int needAccelerateCount;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortAcceptDispatchRespMsg respMsg = BeautyEscortProto.CrossBeautyEscortAcceptDispatchRespMsg.parseFrom(packet.getBytes());
        client = respMsg.getClient();
        crossRet = respMsg.getRet();
        buyList = respMsg.getBuyBeautyList();
        beautyDispatchList = BeautyEscortMgr.parseBeautyDispatch(respMsg.getBeautyList());
        typeId = respMsg.getTypeId();
        cartId = respMsg.getCartId();
        buyBeautyConsume = respMsg.getBuyBeautyConsume();
        dartId = respMsg.getDartId();
        autoAccelerate = respMsg.getAutoAccelerate();
        needAccelerateCount = respMsg.getNeedAccelerateCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        EscortDartTypeInfo typeInfo = ConfigMgr.getDartTypeInfoMap().get(typeId);
        Property consume  = PropertyHelper.parseStringToProperty(typeInfo.getConsume());
        //还原预扣(开启消耗+调令)
        PreReduceModule preReduceModule = player.getModule(PreReduceModule.class);
        if(consume != null && !consume.isNothing()){
            preReduceModule.restorePreReduce(PropertyHelper.parsePropertyToString(consume));
        }
        Property consume2  = PropertyHelper.parseStringToProperty(buyBeautyConsume);
        if(consume2 != null && !consume2.isNothing()){
            preReduceModule.restorePreReduce(PropertyHelper.parsePropertyToString(consume2));
        }
        Property accelerateConsume = null;
        if(autoAccelerate && needAccelerateCount > 0){
            accelerateConsume = new Property(eItemId.BEAUTY_ESCORT_JI_XING_LING.getValue(), needAccelerateCount);
            preReduceModule.restorePreReduce(accelerateConsume);
        }

        //扣除(开启消耗+调令)
        if(crossRet == 0){
            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            if(consume != null && !consume.isNothing()) {
                currencyModule.removeCurrency(consume, eLogMoneyType.BeautyEscort, eLogMoneyType.BeautyEscortOpenConsume);
            }
            if(consume2 != null && !consume2.isNothing()){
                currencyModule.removeCurrency(consume2, eLogMoneyType.BeautyEscort, eLogMoneyType.BeautyEscortDispatchConsume);
            }
            if(null != accelerateConsume){
                currencyModule.removeCurrency(accelerateConsume, eLogMoneyType.BeautyEscort, eLogMoneyType.BeautyEscortAutoAccelerateConsume);
            }

            //押镖日志
            LogBeautyEscorting log;
            //商会等级限制
            UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
            if (unionInfo == null) {
                AutoLogMgr.add(new LogBeautyEscorting(player.getUserId(), cartId, dartId, "", 0, beautyDispatchList, typeInfo.getConsume(),
                        buyBeautyConsume, client, typeId));
            } else {
                int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
                AutoLogMgr.add(new LogBeautyEscorting(player.getUserId(), cartId, dartId, player.getUserInfo().getUnionUid(), unionLv, beautyDispatchList, typeInfo.getConsume(),
                        buyBeautyConsume, client, typeId));
            }
            player.notifyListener(eGamePlayerEventType.BeautyEscortTimes.getValue(),1);
        }else {
            //免费走镖才扣次数
            //美女次数返还
            if(BeautyEscortMgr.isFree(typeInfo)) {
                player.getModule(BeautyEscortModule.class).restoreBeautyData(buyList, beautyDispatchList);
            }
        }

        BeautyEscortProto.BeautyEscortAcceptDispatchRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortAcceptDispatchRespMsg.newBuilder();
        respMsg.setRet(crossRet);
        respMsg.setCartId(cartId);
        player.sendPacket(Protocol.U_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY, respMsg);

        //走镖红点
        BeautyEscortProto.CrossBeautyEscortRedDotReqMsg.Builder reqMsg = BeautyEscortProto.CrossBeautyEscortRedDotReqMsg.newBuilder();
        player.sendPacket(Protocol.C_BEAUTY_ESCORT_RED_DOT_SYNC, reqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortAcceptDispatchRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortAcceptDispatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ACCEPT_DISPATCH_BEAUTY, respMsg);
        }
    }
}
